package com.sky.common.endpoint.matcher;

import com.sky.business.sys.domain.CameraConfig;
import com.sky.common.utils.GeoUtils;
import com.sky.common.utils.NumberUtil;
import com.sky.common.utils.PtCalculator;
import lombok.extern.slf4j.Slf4j;

/**
 * 距离和角度计算追踪能力
 *
 */
@Slf4j
public class DistanceAngleCameraTrackMatcher implements CameraTrackMatcher {


    @Override
    public boolean match(CameraConfig cameraConfig, double uavLng, double uavLat, double uavAltitude) {
        double cameraLat = NumberUtil.parseDouble(cameraConfig.getCameraLatitude());
        double cameraLng = NumberUtil.parseDouble(cameraConfig.getCameraLongitude());
        double cameraAltitude = NumberUtil.parseDouble(cameraConfig.getCameraAltitude());
        double groundDistance = GeoUtils.calculateDistance(cameraLat, cameraLng, uavLat, uavLng);
        double altitudeDistance = Math.abs(uavAltitude-cameraAltitude);
        double distance = Math.sqrt(Math.pow(groundDistance,2)+Math.pow(altitudeDistance,2));
        //距离过近或过远
        if(distance<cameraConfig.getDetectMinDist() || distance>cameraConfig.getDetectMaxDist() ){
            log.warn("无人机不在设备检测范围之内. 设备ID:{},最小检测距离:{}, 最大检测距离:{},无人机距离:{} ",cameraConfig.getCameraDeviceId(), cameraConfig.getDetectMinDist(),cameraConfig.getDetectMaxDist(),distance);
            return false;
        }
        double tiltAngleMin = Math.abs(NumberUtil.parseDouble(cameraConfig.getCameraTiltRangeMin()));
        double tiltAngleMax = Math.abs(NumberUtil.parseDouble(cameraConfig.getCameraTiltRangeMax()));

        //云台垂直没有限制
        if(tiltAngleMin>=90  && tiltAngleMax>=90){
            return true;
        }
        //计算垂直角度
        double verticalDegress = PtCalculator.calVerticalDegrees(groundDistance, cameraAltitude, uavAltitude);
        if(verticalDegress>=0){
            //无人机在摄像机上面,判断摄像机最大仰角是否满足要求
            if(tiltAngleMax<verticalDegress){
                log.warn("无人机追踪超过设备最大仰角. 设备ID:{}, 仰角:{}, 设备最大仰角:{}",cameraConfig.getCameraDeviceId(),Math.abs(verticalDegress), tiltAngleMax);
                return false;
            }
        }else{
            //无人机在摄像机下面,判断摄像机最大俯角是否满足要求
            if(tiltAngleMin<Math.abs(verticalDegress)){
                log.warn("无人机追踪超过设备最大俯角. 设备ID:{}, 俯角:{}, 设备最大俯角:{}",cameraConfig.getCameraDeviceId(),Math.abs(verticalDegress),tiltAngleMin);
                return false;
            }
        }
        return true;
    }
}
